package com.feib.stms.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.support.JdbcUtils;

public class ActionlogXlsResultSetExtractor implements ResultSetExtractor <Object>{

	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	protected Sheet sheet;
	
	protected int rownum;
	
	protected Map<String, CellStyle> styles;

	public ActionlogXlsResultSetExtractor(Sheet sheet, int rownum, Map<String, CellStyle> styles) {
		super();
		this.sheet = sheet;
		this.rownum = rownum;
		this.styles = styles;
	}

	/* (non-Javadoc)
	 * @see org.springframework.jdbc.core.ResultSetExtractor#extractData(java.sql.ResultSet)
	 */
	public Object extractData(ResultSet rs) throws SQLException,
			DataAccessException 
	{
		while (rs.next()) {
			
            // data rows

            Row row = sheet.createRow(rownum++);
            row.setHeightInPoints((float) 24);
            int j = 0;
            
            // 操作日期
            Date inDateTimeValue = (Date)getColumnValue(rs, "IN_DATE_TIME", Date.class);
            Cell inDateTimeCell = row.createCell(j++);
            if (null != inDateTimeValue)
            	inDateTimeCell.setCellValue(inDateTimeValue);
            inDateTimeCell.setCellStyle(styles.get("cell_date"));  

            // 時間
            Date inDateTime2Value = (Date)getColumnValue(rs, "IN_DATE_TIME", Date.class);
            Cell inDateTime2Cell = row.createCell(j++);
            if (null != inDateTime2Value)
            	inDateTime2Cell.setCellValue(inDateTime2Value);
            inDateTime2Cell.setCellStyle(styles.get("cell_date_min"));  

            // 功能模組
            String parentActionNameValue = (String)getColumnValue(rs, "PARENT_ACTION_NAME", String.class);
            Cell parentActionNameCell = row.createCell(j++);
            if (null != parentActionNameValue)
            	parentActionNameCell.setCellValue(parentActionNameValue);
            parentActionNameCell.setCellStyle(styles.get("cell")); 
            
            // 功能名稱 
            String actionNameValue = (String)getColumnValue(rs, "ACTION_NAME", String.class);
            Cell actionNameCell = row.createCell(j++);
            if (null != actionNameValue)
            	actionNameCell.setCellValue(actionNameValue);
            actionNameCell.setCellStyle(styles.get("cell"));
            
            // 操作內容
            String chiEventNameValue = (String)getColumnValue(rs, "chiEventName", String.class);
            Cell chiEventNameCell = row.createCell(j++);
            if (null != chiEventNameValue)
            	chiEventNameCell.setCellValue(chiEventNameValue);
            chiEventNameCell.setCellStyle(styles.get("cell"));

            // 來源IP
            String remoteAddrValue = (String)getColumnValue(rs, "REMOTE_ADDR", String.class);
            Cell remoteAddrCell = row.createCell(j++);
            if (null != remoteAddrValue)
            	remoteAddrCell.setCellValue(remoteAddrValue);
            remoteAddrCell.setCellStyle(styles.get("cell")); 

            // 使用者ID
            String userIdValue = (String)getColumnValue(rs, "USER_ID", String.class);
            Cell userIdCell = row.createCell(j++);
            if (null != userIdValue)
            	userIdCell.setCellValue(userIdValue);
            userIdCell.setCellStyle(styles.get("cell")); 

            // 分公司
            String securitiesName = (String)getColumnValue(rs, "securities_name", String.class);
            Cell stockNoCell = row.createCell(j++);
            if (null != securitiesName)
            	stockNoCell.setCellValue(securitiesName);
            stockNoCell.setCellStyle(styles.get("cell"));

        	}
		
		return null;
	}
	
	/**
     * @return the rownum
     */
    public int getRownum() {
        return rownum;
    }
	
	protected Object getColumnValue(ResultSet rs, String columnName, Class<?> requiredType)
	{
		int index = 0;
		try {
			index = rs.findColumn(columnName);
		} catch (SQLException e) {
			logger.trace("資料集(ResultSet)中資料欄名稱 ["+columnName+"] 無效，請先確認SQL Statement 有Select 此欄位.", e);
		}
		

		try {
			if (index > 0)
				return JdbcUtils.getResultSetValue(rs, index, requiredType);
		} catch (SQLException e) {
			logger.trace("資料集(ResultSet)中資料欄名稱 ["+columnName+"] 欄位值錯誤，可能型別轉型錯誤(需要轉為 " + requiredType + " 型態)", e);
		}
		
		return null;
	}


}
